package SingleLinkedList;

import java.util.Scanner;

public class singleList {

    public static void main(String[] args) {

        SingleLinkedList test = new SingleLinkedList();
        test.addLinkedByOrder(new HeroNode(1,"宋江","及时雨"));
        test.addLinkedByOrder(new HeroNode(4,"林冲","豹子头"));
        test.addLinkedByOrder(new HeroNode(3,"吴用","智多星"));
        test.addLinkedByOrder(new HeroNode(2,"卢俊义","玉麒麟"));
        test.showList();
        System.out.println("删除后的数据为:");
        test.deleteListByNo(1);
        test.deleteListByNo(2);
        test.showList();
        test.updateListByNo(1);
        test.showList();
        test.updateListByNo(6);
    }
}

class SingleLinkedList{
    HeroNode head = new HeroNode(0,null,null);
    public void addLinked(HeroNode heroNode){
        //因为头节点是不能动的 所以需要临时遍历辅助
        HeroNode temp = head;
        while (temp.next!=null){
            temp=temp.next;
        }
        temp.next=heroNode;
    }
    public void addLinkedByOrder(HeroNode heroNode){
        //因为头节点是不能动的 所以需要临时遍历辅助
        HeroNode temp = head;
        boolean isFlag = false;
            while (true) {
                if(temp.next==null){
                    break;
                }
                if(temp.next.no>heroNode.no){
                    break;
                }else if (temp.next.no==heroNode.no){
                    isFlag=true;
                    break;
                }
                temp=temp.next;
            }
        if (isFlag){
            System.out.println("该编号"+heroNode.no+"已经存在");
        }else {
            heroNode.next=temp.next;
            temp.next=heroNode;
        }
    }
    public void showList(){
        if(head.next==null){
            System.out.println("链表为空");
        }
        //因为头节点是不能动的 所以需要临时遍历辅助
        HeroNode temp = head;
        while (temp.next!=null){
            temp=temp.next;
            System.out.println(temp);
        }
        System.out.println("输出完毕");
    }
    public void deleteListByNo(int n){
            HeroNode temp = head;
            if (head.next==null){
                System.out.println("请添加数据后再删除");
            }
            temp=head;
            while (true){
                if(temp.next.no==n){
                    temp.next=temp.next.next;
                    return;
                }
                temp=temp.next;
                if(temp==null){
                    System.out.println("没有该数据，删除失败");
                    return;
                }
            }
    }
    public void updateListByNo(int n){
        if(head.next==null){
            System.out.println("无数据无法修改");
            return;
        }
        HeroNode temp = head.next;
        while (true){
            if(temp.no==n){
                Scanner scanner= new Scanner(System.in);
                System.out.println("输入空格后回车均表示不修改保留原值");
                System.out.println("原名为："+temp.name);
                System.out.println("您要将编号为"+n+"的名字修改为:");
                String name = scanner.nextLine();
                if(name!=null && !name.contains(" ")){
                      temp.name=name;
                    System.out.println("名字修改完成为:"+temp.name);
                }else {
                    System.out.println("您已经取消修改,现名为:"+temp.name);
                }
                System.out.println("您要将其昵称改为：");
                String nickName = scanner.nextLine();
                if(nickName!=null && !nickName.contains(" ")){
                    temp.nickName=nickName;
                    System.out.println("昵称修改完成为"+temp.nickName);
                }else {
                    System.out.println("您已经取消修改,现昵称为:"+temp.nickName);
                }
                break;
            }
            temp=temp.next;
            if(temp==null){
                System.out.println("没有找到该数值");
                return;
            }
        }
    }
    public int lenght(){
        HeroNode temp = head;
        if (temp.next==null){
            return 0;
        }
        int count=0;
        while (true){
            if (temp.next!=null){
                count++;
            }
            temp=temp.next;
            if (temp==null){
                return count;
            }
        }
    }
}

class HeroNode{
    public int no;
    public String name;
    public String nickName;
    public HeroNode next; //指向下一个节点

    public HeroNode(int no, String name, String nickName) {
        this.no = no;
        this.name = name;
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }
}
